﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using BSCMFF.Web.UserControls;

namespace BSCMFF.Web.Pages.Surveys
{
    public partial class SurveyReport : MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SurveySelectionReport.PublicSelectedValue != 0)
            {
                PanelSurveyReport.Visible = true;

                BSCEntities context = new BSCEntities();

                int sfID = SurveySelectionReport.PublicSelectedValue;

                var data = (from srr in context.Survey_Results_Report
                            where srr.Survey_Form_FK == sfID
                            join sa in context.Survey_Answer on srr.Survey_Answer_FK equals sa.ID
                            join sq in context.Survey_Question on srr.Survey_Question_FK equals sq.ID

                            select new
                            {
                                questionID = sq.ID,
                                question = sq.Question,
                                answerID = sa.ID,
                                answer = sa.Answer,
                                number = srr.Number
                            }).ToList();

                int i = 0;
                while (i < data.Count)
                {
                    string question = data[i].question;
                    int questionID = data[i].questionID;
                    Dictionary<string, int> results = new Dictionary<string, int>();
                    results.Add(data[i].answer, data[i].number);

                    ++i;
                    while (i < data.Count && data[i].questionID == questionID)
                    {
                        results.Add(data[i].answer, data[i].number);
                        ++i;
                    }
                    SurveyQuestionChart sqch = LoadControl("~/UserControls/SurveyQuestionChart.ascx") as SurveyQuestionChart;
                    sqch.ID = questionID.ToString();
                    sqch.Question = question;
                    sqch.Results = results;
                    PanelSurveyReport.Controls.Add(sqch);
                }



                //var data2 = (from sr in context.Survey_Results.DefaultIfEmpty()
                //            join sa in context.Survey_Answer on sr.Answer_FK equals sa.ID
                //            join sq in context.Survey_Question on sa.Question_FK equals sq.ID
                //            where sq.Form_FK == sfID
                //            select sr).ToList();

               /* //pokus o left outer join .. 
                var data = (from sq in context.Survey_Question
                            where sq.Form_FK == sfID
                            join sa in context.Survey_Answer on sq.ID equals sa.Question_FK
                            join sr in context.Survey_Results.DefaultIfEmpty() on sa.ID equals sr.Answer_FK into sa_sr

                            from sr in sa_sr.DefaultIfEmpty()

                            group sa by new { sa.Question_FK, sa.ID } into g
                            orderby g.Key.Question_FK, g.Key.ID

                            

                            //select sq).ToList();
                            //group sr by new { sr.Survey_Answer.Question_FK, sr.Survey_Answer.ID } into g
                           // select g).ToList(); 
                            //orderby g.Key.Question_FK, g.Key.ID
                            select new
                            {
                                questionID = g.Key.Question_FK,
                                answerID = g.Key.ID,
                                //count = g.Distinct().Count()
                                count = g.Select(sa => sa.Survey_Results.Where(sr => sr.ID != 11111)).Count(),
                                count2 = g.Select(sa => sa.Survey_Results.FirstOrDefault(sr => sr.ID == 1)).Count(),
                                //count3 = g.Where(sa => sa.Survey_Results.Where(
                                //count2 = g.Distinct().Count(
                                //ae.Select(n => n.Ccy).Distinct().Count()
                            }).ToList();
                */
                //inner join (budu chybat odpovede, ktore nikto neklikol
          /*      var data = (from sq in context.Survey_Question
                            where sq.Form_FK == sfID
                            join sa in context.Survey_Answer on sq.ID equals sa.Question_FK
                            join sr in context.Survey_Results on sa.ID equals sr.Answer_FK 
                            group sr by new { sr.Survey_Answer.Question_FK, sr.Survey_Answer.ID } into g
                            orderby g.Key.Question_FK, g.Key.ID
                            join sqq in context.Survey_Question on g.Key.Question_FK equals sqq.ID
                            join saa in context.Survey_Answer on g.Key.ID equals saa.ID
                            select new
                            {
                                questionID = g.Key.Question_FK,
                                question = sqq.Question,
                                answerID = g.Key.ID,
                                answer = saa.Answer,
                                count1 = g.Count()
                                //count = g.Select(sa => sa.Survey_Results).Count(),
                                
                            }).ToList();

                if(data.Count != 0)
                {
                    int i = 0;
                    while(i<data.Count)
                    {
                        string question = data[i].question;
                        int questionID = data[i].questionID;
                        ++i;
                        while(i<data.Count && data[i].questionID == questionID)
                        {

                        }
                    }
                    

                int qID = data.First().questionID;
                Dictionary<string, int> results = new Dictionary<string, int>();
                

                foreach (var d in data)
                {
                    if (qID != d.questionID)
                    {
                        qID = d.questionID;

                        SurveyQuestionChart sqch = LoadControl("~/UserControls/SurveyQuestionChart.ascx") as SurveyQuestionChart;
                        sqch.ID = qID.ToString();
                        sqch.Question = d.question;

                        results
                        results.Add(d.answer, d.count1);
                    }
                    else
                    {

                    }

                }
                }
                */
                //RadGrid rg = new RadGrid();
                //rg.DataSource = data;
                //rg.DataBind();

                //Page.Controls.Add(rg);
            }
        }
    }
}